#!/bin/bash

read -p "请输入备份文件名（例如：mybackup.tar.gz,默认时间.后缀）：" backup_filename

if [ -z "$backup_filename" ]; then
  backup_filename="backup_$(date +%Y_%m_%d_%H_%M_%S).tar.gz"
fi

while true; do
    read -p "请输入需要备份的容器目录（例如：/data/data/com.termux/files，直接回车则默认files）：" container_dir
    

    if [ -z "/data/data/com.termux/$container_dir" ]; then
      container_dir="files"
    fi
    

    if [ -d "/data/data/com.termux/$container_dir" ]; then
        break
    else
        echo "错误：指定的容器目录不存在：$container_dir，请重新输入！"
    fi
done


backup_path="/storage/shared/xinhao/data/$backup_filename"


tar -zcvf .$backup_path /data/data/com.termux/$container_dir


if [ $? -eq 0 ]; then
    echo "备份完成，备份文件已保存在：/storage/shared/xinhao/data/"
    echo "备份文件路径: $backup_path"
else
    echo "备份失败，请检查是否有错误信息"
    exit 1
fi